let path = require('path')
let webpack = require('webpack')

module.exports = {
    context: path.join(__dirname, './public/src'),
    entry: {
        main: './main.js'
    },
    devtool: 'eval-source-map',
    output: {
        // 编译输出的静态资源根路径
        path: path.join(__dirname, './public/dist'),
        filename: "[name].js",
        publicPath: '/dist'
    },
    resolveLoader: {
        modules: [path.join(__dirname, './node_modules')]
    },
    resolve: {
        modules: [path.join(__dirname, './node_modules')],
        // 自动补全的扩展名
        extensions: ['.js', '.vue', '.json'],
        alias: {
            // 例如 import Vue from 'vue'，会自动到 'vue/dist/vue.common.js'中寻找
            'vue$': 'vue/dist/vue.esm.js',
            // '@': resolve('src'),  //文件别名 @会指向src,在import路径中可以使用@/开头，在各层目录中使用../
        }
    },
    module: {
        rules: [{
            test: /\.vue$/,
            loader: 'vue-loader',
            options: {
                loaders: {
                    js: 'babel-loader'
                }
            }
        },
        {
            test: /\.pug$/,
            use: ['raw-loader', 'pug-html-loader']
        },
        {
            test: /\.html$/,
            use: {
                loader: 'html-loader',
                options: {
                    attrs: false
                }
            }
        },
        {
            test: /\.less$/,
            use: ["style-loader", "css-loader", { loader: 'postcss-loader' }, "less-loader"]
        },
        {
            test: /\.js$/,
            include: path.join(__dirname, './public/src'),
            exclude: [
                path.join(__dirname, './node_modules')
            ],
            use: {
                loader: 'babel-loader',
                options: {
                    presets: [
                        'es2015', [
                            'env',
                            {
                                targets: {
                                    browsers: ['last 2 versions'],
                                },
                            },
                        ],
                        'stage-2',
                    ],
                    "comments": true,
                    "plugins": [
                        [
                            'transform-runtime',
                            {
                                polyfill: false,
                                regenerator: true,
                            },
                        ],
                    ]
                }
            }
        },
        {
            test: /\.json$/,
            loader: 'json-loader'
        },
        {
            test: /\.(jpe?g|png|gif|svg)$/i,
            loader: 'base64-image-loader'
        },
        {
            test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
            use: {
                loader: 'url-loader'
            }
        },
        {
            test: /\.css$/,
            loader: ['style-loader', 'css-loader'] //, { loader: 'postcss-loader' }
        }]
    },
    watchOptions: {
        ignored: path.join(__dirname, './node_modules')
    },
    plugins: [
        new webpack.optimize.OccurrenceOrderPlugin(), //排序输出通过模块调用次数给模块分配ids，常用的ids就会分配更短的id，使ids可预测，减小文件大小
        new webpack.NoEmitOnErrorsPlugin()  //遇到错误继续的一个webpack插件
    ]
}